package com.gx.smart.building;

import com.casic.gx.grpc.common.ComQuery;
import com.gx.smart.lib.http.api.asynctask.CallBack;
import com.gx.smart.lib.http.api.asynctask.GrpcAsyncTask;
import com.gx.smart.lib.http.api.config.ApiConfig;
import com.gx.smart.lib_http.BuildConfig;
import com.huishi.gx.ib.grpc.lib.ib.app.knowledge.v1.KnowledgeApiGrpc;
import com.huishi.gx.ib.grpc.lib.ib.app.knowledge.v1.KnowledgeBaseDto;
import com.huishi.gx.ib.grpc.lib.ib.app.knowledge.v1.QueryKnowledgeDetailReq;
import com.huishi.gx.ib.grpc.lib.ib.app.knowledge.v1.QueryKnowledgeDetailResp;
import com.huishi.gx.ib.grpc.lib.ib.app.knowledge.v1.QueryKnowledgeListReq;
import com.huishi.gx.ib.grpc.lib.ib.app.knowledge.v1.QueryKnowledgeListResp;
import com.orhanobut.logger.Logger;

import java.util.concurrent.TimeUnit;

import io.grpc.ManagedChannel;

/**
 * @author: xiaosy
 * 时间: 2022/4/7
 * @Description: java类作用描述
 * @Version: 1.0
 **/
public class KnowledgeApiService {
    private String host = BuildConfig.JOYLIFE_GRPC_UAA_SERIVER_URL;
    private String port = "80";

    private static class SingleTonHolder {
        private static final KnowledgeApiService INSTANCE = new KnowledgeApiService();
    }

    public static KnowledgeApiService getInstance() {
        return SingleTonHolder.INSTANCE;
    }

    private KnowledgeApiService() {

    }

    /**
     * 获取AuthStub  设置超时时间  8秒
     *
     * @param channel
     * @return
     */
    private KnowledgeApiGrpc.KnowledgeApiBlockingStub getStub(ManagedChannel channel) {
        return KnowledgeApiGrpc.newBlockingStub(channel)
                .withDeadlineAfter(ApiConfig.TIMEOUT_NETWORK, TimeUnit.SECONDS);

    }

    /**
     * 查询文章列表
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, QueryKnowledgeListResp> queryKnowledgeList(
            ComQuery comQuery,
            String baseTypeId,
            CallBack<QueryKnowledgeListResp> callBack) {
        return new GrpcAsyncTask<String, Void, QueryKnowledgeListResp>(callBack) {
            @Override
            protected QueryKnowledgeListResp doRequestData(ManagedChannel channel) {
                QueryKnowledgeListReq message = QueryKnowledgeListReq.newBuilder()
                        .setComQuery(comQuery)
                        .setTenantNo(1)
                        .setKnowledgeDto(KnowledgeBaseDto.newBuilder().setBaseTypeId(baseTypeId).build())
                        .build();
                Logger.d(message);
                QueryKnowledgeListResp response = null;
                try {
                    response = getStub(channel).queryKnowledgeList(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "queryKnowledgeList");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 查看文章详情
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, QueryKnowledgeDetailResp> queryKnowledgeDetail(
            String id,
            CallBack<QueryKnowledgeDetailResp> callBack) {
        return new GrpcAsyncTask<String, Void, QueryKnowledgeDetailResp>(callBack) {
            @Override
            protected QueryKnowledgeDetailResp doRequestData(ManagedChannel channel) {
                QueryKnowledgeDetailReq message = QueryKnowledgeDetailReq.newBuilder()
                        .setId(id)
                        .setTenantNo(1)
                        .build();
                Logger.d(message);
                QueryKnowledgeDetailResp response = null;
                try {
                    response = getStub(channel).queryKnowledgeDetail(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "queryKnowledgeDetail");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }
}
